#pragma once

#include <Windows.h>
#include <MSWSock.h>
#include <assert.h>
#include <string.h>
#include "Type.h"

#define WM_USER_SOCKET	WM_USER + 1
#define SYNBUFSIZE 1024

class ClientManager
{
public:
	ClientManager(void);
	virtual ~ClientManager(void);

	BOOL Connect(HWND m_hWnd, char *host, char *port);
	void Listen(HWND m_hWnd);
	int Receive(SOCKET s);
	void SetState(int state);
	int GetState(void) { return m_nState; }
	int16 GetDataPort(void) { return m_DataPort; }
	void SetUser(char *user, UINT len);
	void SetPassword(char *password, UINT len);
	void SetDataState(int data);
	int GetDataState(void) { return m_nDataState; }
	void Close(void);
	void SendPWD(void);
	void SetFileName(char *fn, UINT len);
	void ChangeDirectory(bool CWD);

private:
	SOCKET m_nConnfd;
	SOCKET m_nListenfd;
	sockaddr_in m_kSockaddr;
	int16 m_ListenPort;
	int16 m_DataPort;
	int m_nState;
	int m_nRecvNum;
	char *m_cpCpy;
	BOOL m_bSameSystem;
	BOOL m_bConnect;
	char m_cUser[128 + 1];
	char m_cPassword[128 + 1];
	char m_cIP[16];
	char m_cFileName[SYNBUFSIZE + 1];
	int m_nDataState;
	char m_cServPath[SYNBUFSIZE + 1];
	bool m_bCWDfake;
	
	//recvline
	int m_nRecvByte;
	int m_nCopyByte;
	char m_cRecvBuf[SYNBUFSIZE + 1];
	char m_cCopyBuf[SYNBUFSIZE + 1];
	char *m_cpRecv;
	char *m_cpCopy;
	BOOL m_bRecvError;

	//enums
	struct m_kEnum
	{
		enum
		{
			ERROR_STATE = -1,
			NONE,
			CONNECT,
			CONNECT_WAIT,
			SYST,
			PASV,
			PORT_STATE,
			QUIT,
			USER,
			PASS,
			CWD,
			TYPE,
			FILEUP,
			FILEDOWN,
			FILE_WAIT,
			PWD,
			CDUP,
			MLSD,
			NEED,
			SUCCESS,
			WAIT,
			FAILED,
			DISCONNECT
			//add more when I need it
		};
	};

	int ResponseAnalysis(void);
	void DataRequest(int type);
	void SetDataPort(void);

protected:

};

